package com.xxcxcxcx.jobsystem.common.Job.Cook;

import com.xxcxcxcx.jobsystem.Main;
import com.xxcxcxcx.jobsystem.PlayerSaveData;
import com.xxcxcxcx.jobsystem.common.Job.JobBase;
import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapability;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.food.Foods;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.ModList;
import vectorwing.farmersdelight.common.registry.ModItems;

public class Cook extends JobBase {
    public static String MakeFood=Main.MOD_ID+".cook.makefood";
    public static String Newnutrition="Newnutrition";
    public static String Newsaturation="NewsaturationModifier";
    public static String NewisMeat="NewisMeat";
    public static String NewcanAlwaysEat="NewcanAlwaysEat";
    public static String NewfastFood="NewfastFood";
    public static String Effect = "Effect";
    public static String EffectTime = "EffectTime";
    public static String EffectLevel = "EffectLevel";
    public static String EffectChance = "EffectChance";
    public static String Job = JobsClass.Cook;
    public static String Cooker = "Cook";
    public static String CookLevel = "CookLevel";
    public static String FoodLevel = "FoodLevel";
    public static String UnIdentification = "UnIdentification";
    public static final String[] FoodLevels = {"bad","ordinary","skilled","innovative","perfect"};
    public static final Item[] basefood= {Items.CARROT,Items.APPLE,Items.POTATO,Items.BEETROOT};
    public static void GetCook(Player player, ItemStack itemStack) {
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        if(RightFood(itemStack))return;
        int cooklevel=playerSaveData.getLevel(Job);
        SetFood setfood =(SetFood)itemStack.getItem();
        setfood.setCook(itemStack,player,cooklevel);
    }
    public static boolean RightFood(ItemStack itemStack){
        Item food= itemStack.getItem();
        for(Item item : basefood) {
            if (food == item) return true;
        }
        if(FarmRightFood(itemStack))return true;
        return false;
    }
    public static boolean FarmRightFood(ItemStack itemStack){
        if(ModList.get().isLoaded("farmersdelight")) {
            Item food= itemStack.getItem();
            Item[] Farmbasefood= {ModItems.CABBAGE.get(),ModItems.TOMATO.get(),ModItems.ONION.get()};
            for(Item item : Farmbasefood) {
                if (food == item) return true;
            }
        }
        return false;
    }
    public static void IdentificationFood(Player player, ItemStack itemStack){
        int cooklevel=itemStack.getOrCreateTagElement(Main.MOD_ID).getInt(MakeFood+CookLevel);
        int num;
        if(cooklevel==0){
            num=1;
        }else if(cooklevel<4){
            num=2;
        }else if(cooklevel<7){
            num=4;
        }else if(cooklevel<10){
            num=8;
        }else{
            num=16;
        }
        num=Math.min(num,itemStack.getCount());
        ItemStack newitemstack=itemStack.copy();
        newitemstack.setCount(num);
        newitemstack.getOrCreateTagElement(Main.MOD_ID).remove(MakeFood+FoodLevel);
        newitemstack.getOrCreateTagElement(Main.MOD_ID).remove(MakeFood+CookLevel);
        if(cooklevel==0){
            CookFood(newitemstack,player,0,newitemstack.getCount());
        }else if(cooklevel<4){
            double r=player.getRandom().nextDouble();
            if(r<cooklevel*0.2){
                CookFood(newitemstack,player,1,newitemstack.getCount());
            }else{
                CookFood(newitemstack,player,0,newitemstack.getCount());
            }
        }else if(cooklevel<7){
            double r=player.getRandom().nextDouble();
            if(r<(cooklevel-4)*0.2){
                CookFood(newitemstack,player,2,newitemstack.getCount());
            }else if(r<(1-(7-cooklevel)*0.05)){
                CookFood(newitemstack,player,1,newitemstack.getCount());
            }else{
                CookFood(newitemstack,player,0,newitemstack.getCount());
            }
        }else if(cooklevel<10){
            double r=player.getRandom().nextDouble();
            if(r<(cooklevel-6)*0.1){
                CookFood(newitemstack,player,3,newitemstack.getCount());
            }else if(r<((cooklevel-4)*0.15+0.1)){
                CookFood(newitemstack,player,2,newitemstack.getCount());
            }else{
                CookFood(newitemstack,player,1,newitemstack.getCount());
            }
        }else{
            double r=player.getRandom().nextDouble();
            if(r<0.1){
                CookFood(newitemstack,player,4,newitemstack.getCount());
            }else if(r<Math.min((cooklevel-6)*0.1,0.9)){
                CookFood(newitemstack,player,3,newitemstack.getCount());
            }else if(r<(Math.min((cooklevel-4)*0.15+0.1,0.95))){
                CookFood(newitemstack,player,2,newitemstack.getCount());
            }else{
                CookFood(newitemstack,player,1,newitemstack.getCount());
            }
        }
        if(player.getInventory().getSlotWithRemainingSpace(newitemstack)!=-1||player.getInventory().getFreeSlot()!=-1){
            player.addItem(newitemstack);
            itemStack.setCount(itemStack.getCount()-num);
        }else{
            Block.popResource(player.level,new BlockPos(player.position()), newitemstack);
            itemStack.setCount(itemStack.getCount()-num);
        }
    }
    public static void CookFood(ItemStack itemStack,Player player,int GLevel,int num){
        SetFood setfood =(SetFood)itemStack.getItem();
        UpExperience(player,(GLevel+1)*2*num, Job);
        //setfood.setCook(itemStack,player);
        FoodProperties oldfoodProperties = itemStack.getItem().getFoodProperties();
        itemStack.getOrCreateTagElement(Main.MOD_ID).putString(MakeFood+FoodLevel,FoodLevels[GLevel]);
        int Nut,set,ans;
        switch (GLevel){
            case 0:
                Nut=oldfoodProperties.getNutrition()/2;
                setfood.setNewnutrition(itemStack,-Nut);
                set=Math.round(oldfoodProperties.getSaturationModifier()/2*10);
                setfood.setNewsaturationModifier(itemStack,-set/10.0f);
                if(player.getRandom().nextDouble()<0.3f)setfood.setEffect(itemStack,MobEffects.CONFUSION,200,0,1);
                if(player.getRandom().nextDouble()<0.3f)setfood.setEffect(itemStack,MobEffects.BLINDNESS,200,0,1);
                if(player.getRandom().nextDouble()<0.05f)setfood.setEffect(itemStack,MobEffects.POISON,100,0,1);
                if(player.getRandom().nextDouble()<0.05f)setfood.setEffect(itemStack,MobEffects.HUNGER,100,0,1);
                break;
            case 1:
                Nut=oldfoodProperties.getNutrition()/4;
                setfood.setNewnutrition(itemStack,-Nut);
                set=Math.round(oldfoodProperties.getSaturationModifier()/4*10);
                setfood.setNewsaturationModifier(itemStack,-set/10.0f);
                if(player.getRandom().nextDouble()<0.1f)setfood.setEffect(itemStack,MobEffects.REGENERATION,200,0,1);
                if(player.getRandom().nextDouble()<0.05f)setfood.setEffect(itemStack,MobEffects.HEAL,1,0,1);
                break;
            case 2:
                Nut=oldfoodProperties.getNutrition()/4;
                setfood.setNewnutrition(itemStack,Nut);
                set=Math.round(oldfoodProperties.getSaturationModifier()/4*10);
                setfood.setNewsaturationModifier(itemStack,set/10.0f);
                if(player.getRandom().nextDouble()<0.055f)setfood.setEffect(itemStack,MobEffects.REGENERATION,300,0,1);
                if(player.getRandom().nextDouble()<0.055f)setfood.setEffect(itemStack,MobEffects.HEAL,1,0,1);
                if(player.getRandom().nextDouble()<0.055f)setfood.setEffect(itemStack,MobEffects.MOVEMENT_SPEED,600,0,1);
                if(player.getRandom().nextDouble()<0.055f)setfood.setEffect(itemStack,MobEffects.JUMP,600,0,1);
                break;
            case 3:
                if(player.getRandom().nextDouble()<0.8){
                    Nut=oldfoodProperties.getNutrition()/2;
                    setfood.setNewnutrition(itemStack,Nut);
                    set=Math.round(oldfoodProperties.getSaturationModifier()/2*10);
                    setfood.setNewsaturationModifier(itemStack,set/10.0f);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.REGENERATION,600,0,1);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.HEAL,1,0,1);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.MOVEMENT_SPEED,1200,0,1);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.JUMP,1200,0,1);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.ABSORPTION,1200,0,1);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.DAMAGE_RESISTANCE,1200,0,1);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.DAMAGE_BOOST,1200,0,1);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.DIG_SPEED,1200,0,1);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.INVISIBILITY,1200,0,1);
                    if(player.getRandom().nextDouble()<0.035f)setfood.setEffect(itemStack,MobEffects.FIRE_RESISTANCE,1200,0,1);
                }else{
                    setfood.setNewnutrition(itemStack,1-oldfoodProperties.getNutrition());
                    set=Math.round(oldfoodProperties.getSaturationModifier()/2*10);
                    setfood.setNewsaturationModifier(itemStack,-set/10.0f-oldfoodProperties.getSaturationModifier());
                    if(player.getRandom().nextDouble()<0.074f)setfood.setEffect(itemStack,MobEffects.CONFUSION,1200,0,1);
                    if(player.getRandom().nextDouble()<0.074f)setfood.setEffect(itemStack,MobEffects.BLINDNESS,1200,0,1);
                    if(player.getRandom().nextDouble()<0.074f)setfood.setEffect(itemStack,MobEffects.POISON,600,0,1);
                    if(player.getRandom().nextDouble()<0.074f)setfood.setEffect(itemStack,MobEffects.HUNGER,1200,0,1);
                    if(player.getRandom().nextDouble()<0.074f)setfood.setEffect(itemStack,MobEffects.MOVEMENT_SLOWDOWN,1200,0,1);
                    if(player.getRandom().nextDouble()<0.074f)setfood.setEffect(itemStack,MobEffects.WITHER,600,0,1);
                    if(player.getRandom().nextDouble()<0.074f)setfood.setEffect(itemStack,MobEffects.GLOWING,1200,0,1);
                    if(player.getRandom().nextDouble()<0.074f)setfood.setEffect(itemStack,MobEffects.HARM,1,0,1);
                    if(player.getRandom().nextDouble()<0.074f)setfood.setEffect(itemStack,MobEffects.DIG_SLOWDOWN,1200,0,1);
                }
                break;
            case 4:
                if(player.getRandom().nextDouble()<0.5){
                    Nut=oldfoodProperties.getNutrition();
                    setfood.setNewnutrition(itemStack,Nut);
                    set=Math.round(oldfoodProperties.getSaturationModifier()*10);
                    setfood.setNewsaturationModifier(itemStack,set/10.0f);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.REGENERATION,1800,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.HEAL,3,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.MOVEMENT_SPEED,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.JUMP,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.ABSORPTION,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.DAMAGE_RESISTANCE,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.DAMAGE_BOOST,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.DIG_SPEED,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.INVISIBILITY,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.FIRE_RESISTANCE,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.HEALTH_BOOST,3600,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.BEEF)setfood.setEffect(itemStack,MobEffects.DAMAGE_BOOST,1800,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.COOKED_PORKCHOP)setfood.setEffect(itemStack,MobEffects.ABSORPTION,1800,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.COOKED_CHICKEN)setfood.setEffect(itemStack,MobEffects.SLOW_FALLING,1800,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.COOKED_RABBIT)setfood.setEffect(itemStack,MobEffects.MOVEMENT_SPEED,1800,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.COOKED_MUTTON)setfood.setEffect(itemStack,MobEffects.DIG_SPEED,1800,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.MUSHROOM_STEW)setfood.setEffect(itemStack,MobEffects.NIGHT_VISION,1800,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.RABBIT_STEW)setfood.setEffect(itemStack,MobEffects.JUMP,1800,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.BEETROOT_SOUP)setfood.setEffect(itemStack,MobEffects.HEAL,1,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.PUMPKIN_PIE)setfood.setEffect(itemStack,MobEffects.REGENERATION,900,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.COOKED_SALMON)setfood.setEffect(itemStack,MobEffects.WATER_BREATHING,1800,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.COOKED_COD)setfood.setEffect(itemStack,MobEffects.WATER_BREATHING,1800,0,1);
                    if(itemStack.getItem().getFoodProperties()== Foods.COOKIE)setfood.setEffect(itemStack,MobEffects.LUCK,1800,0,1);
                }else{
                    setfood.setNewnutrition(itemStack,-oldfoodProperties.getNutrition()*2);
                    setfood.setNewsaturationModifier(itemStack,-oldfoodProperties.getSaturationModifier()*2);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.CONFUSION,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.BLINDNESS,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.POISON,1800,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.HUNGER,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.MOVEMENT_SLOWDOWN,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.WITHER,1800,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.GLOWING,3600,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.HARM,3,0,1);
                    if(player.getRandom().nextDouble()<0.125f)setfood.setEffect(itemStack,MobEffects.DIG_SLOWDOWN,3600,0,1);
                }
                break;
        }
    }
}
